<?php
//租还记录
namespace app\ausagent\controller;
use app\ausagent\controller\Common;
use think\Request;
use think\Db;
use think\File;
use think\Session;
use think\Cookie;
use Godok\Org\FileManager;
class Leaseorder extends Common
{
	public function index(){
		$u = 'user';//定义用户表
		$s = 'seller';//定义商家表
		$pages = 20;//定义默认显示每页显示记录数
		$where = 'a.is_delete = 1 AND a.oid = '.session('oid');
		$pageParam    = ['query' =>[]];
		//判断是否有查询订单好
		if(!empty(input('order_no'))){
			$where .= ' AND a.order_no = '.input('order_no');
			$pageParam['query']['order_no'] = input('order_no');	
		}
		//判断是否有查询指定用户名
		if(!empty(input('name'))){
			$where .= ' AND b.name LIKE '."'%".input('name')."%' ";
			$pageParam['query']['name'] = input('name');
		}
		//判断是否有查询商户名称
		if(!empty(input('sname'))){
			$where .= ' AND c.name LIKE '."'%".input('sname')."%' ";
			$pageParam['query']['sname'] = input('sname');
		}
		//判断是否有选择按订单状态
		if(!empty(input('status'))){
			$where .= ' AND a.status = '.input('status');
			$pageParam['query']['status'] = input('status');
		}
		$list=Db::name('leaseorder')
			->alias('a')
			->join("$u b",'a.oid = b.oid and a.uid = b.id')
			->join("$s c",'a.sid = c.id and a.oid = c.oid')
			->field('a.id,a.order_no,a.name as lname,a.starttime,a.endtime,a.amount,a.status,a.billingunit,a.payable_amount,a.cabinetid,a.deviceid,a.create_time,b.name,b.picture,a.uid,a.sid,c.name as sname')
			->where($where)
			->order('a.starttime DESC')
			->paginate($pages, false, $pageParam);
		$this->assign('list',$list);
		
		return $this->fetch();
	}
	//详细信息及更新信息
	public function edit($id='', $data= ''){

		//echo $id; die;
		$id = Request::instance()->get('id','','number_int');
		$u = 'user';//定义用户表
		$s = 'seller';//定义商家表
		$where = 'a.id = '.$id;
		$list=Db::name('leaseorder')
			->alias('a')
			->join("$u b",'a.oid = b.oid and a.uid = b.id')
			->join("$s c",'a.sid = c.id and a.oid = c.oid')
			->field('a.id,a.order_no,a.name as lname,a.starttime,a.endtime,a.amount,a.status,a.billingunit,a.payable_amount,a.cabinetid,a.deviceid,a.create_time,a.remarks,b.name,b.picture,a.uid,a.sid,c.name as sname')
			->where($where)
			->find();
			if ( Request::instance()->isPost() ) {
			$data['id'] = input('id');
			$data['endtime'] = strtotime(input('endtime'));
			$data['status'] = input('status');
			$data['remarks'] = input('remarks');
			//var_dump($data); die;
			if(Db::name( 'leaseorder')->update($data)){
				$oid = session('oid');$m = 'ausagent'; $c = 'Leaseorder'; $action="edit"; $uid=session('id'); $querystring = '更新租还记录信息'.$id;$ip = get_ip();$os = get_os();
				set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
    			$this->redirect('index');
    			//return ['code'=>0, 'msg'=>'删除成功！'];
    		}else{
    			//return ['code'=>-1050, 'msg'=>'删除失败！'];
				$this->error('修改失败！');
    		}
    		return;	
		}
			$this->assign('list',$list);

		return $this->fetch();
	}

	public function delete(){

	}
}